package edu.jabs.patientsCentral.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import edu.jabs.patientsCentral.domain.Patient;

/**
 * Panel showing the list of registered patients
 */
public class PatientListPanel extends JPanel implements ActionListener, ListSelectionListener
{
    // -----------------------------------------------------------------
    // Constants
    // -----------------------------------------------------------------

    private static final String ADD = "Add Patient";

    private static final String SEARCH = "Search";

    private static final String DELETE = "Delete";

    // -----------------------------------------------------------------
    // Fields
    // -----------------------------------------------------------------

    /**
     * It is a reference to the main window
     */
    private PatientsCentralGUI principal;

    // -----------------------------------------------------------------
    // GUI Fields
    // -----------------------------------------------------------------
    /**
     * Component to display the list of patients
     */
    private JList patientList;

    /**
     * Scroll Bar of the List
     */
    private JScrollPane listScrollBar;

    /**
     * Button to insert a patient
     */
    private JButton buttonAdd;

    /**
     * Button to delete a patient
     */
    private JButton buttonDelete;

    /**
     * Button to search for a patient
     */
    private JButton buttonSearch;

    // -----------------------------------------------------------------
    // Constructors
    // -----------------------------------------------------------------

    /**
     * Panel constructor
     * @param window Reference to the main window
     */
    public PatientListPanel( PatientsCentralGUI window )
    {
        principal = window;

        setLayout( new GridBagLayout( ) );
        setBorder( new TitledBorder( "Patients" ) );

        // List of patients
        patientList = new JList( );
        patientList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
        patientList.addListSelectionListener( this );
        listScrollBar = new JScrollPane( );
        listScrollBar.setViewportView( patientList );
        GridBagConstraints gbc = new GridBagConstraints( );
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets( 0, 0, 0, 10 );
        add( listScrollBar, gbc );

        JPanel buttonsPanel = new JPanel( );
        buttonsPanel.setLayout( new GridLayout( 3, 1, 10, 10 ) );

        // Buttons
        buttonAdd = new JButton( );
        buttonAdd.setActionCommand( ADD );
        buttonAdd.addActionListener( this );
        buttonAdd.setIcon( new ImageIcon( "data/new.gif" ) );
        buttonAdd.setToolTipText( "Add Patient" );
        buttonsPanel.add( buttonAdd );

        buttonSearch = new JButton( );
        buttonSearch.setActionCommand( SEARCH );
        buttonSearch.addActionListener( this );
        buttonSearch.setIcon( new ImageIcon( "data/search.gif" ) );
        buttonSearch.setToolTipText( "Search Patient" );
        buttonsPanel.add( buttonSearch );

        buttonDelete = new JButton( );
        buttonDelete.setActionCommand( DELETE );
        buttonDelete.addActionListener( this );
        buttonDelete.setIcon( new ImageIcon( "data/delete.gif" ) );
        buttonDelete.setToolTipText( "Delete Patient" );
        buttonsPanel.add( buttonDelete );

        gbc = new GridBagConstraints( );
        gbc.gridx = 1;
        gbc.gridy = 0;
        add( buttonsPanel, gbc );

    }

    // -----------------------------------------------------------------
    // Methods
    // -----------------------------------------------------------------
    /**
     * It updates the list of patients
     * @param patients The list of patients to be displayed
     */
    public void updateList( ArrayList patients )
    {
        patientList.setListData( patients.toArray( ) );
    }

    /**
     * It returns the selected patient on the list. If there is no selected patient, it returns null
     * @return The selected patient. Null if no patient is selected
     */
    private Patient getSelectedPatient( )
    {
        return ( Patient )patientList.getSelectedValue( );

    }

    /**
     * Handling button events
     * @param e Event generated by the action - e != null.
     */
    public void actionPerformed( ActionEvent e )
    {
        String command = e.getActionCommand( );

        if( command.equals( ADD ) )
        {
            principal.showAddPatientOptionsDialogue( );
        }
        else if( command.equals( SEARCH ) )
        {
            principal.searchPatient( );
        }
        else if( command.equals( DELETE ) )
        {
            principal.deletePatient( );
        }
    }

    /**
     * It shows the information of the selected patient
     * @param e The event that changed the selected item on the list
     */
    public void valueChanged( ListSelectionEvent e )
    {

        if( getSelectedPatient( ) != null )
        {
            principal.showPatientInformation( getSelectedPatient( ) );
        }
    }
}
